<template>
	<p>
		<a-space style="float: right; margin-bottom: 20px">
			<a-button type="primary" @click="handleQuery()">刷新</a-button>
			<a-button type="primary" @click="onAdd">新增</a-button>
		</a-space>
	</p>
	<a-table :columns="columns"
	         :dataSource="passengerList"
	         :loading="loading"
	         :pagination="pagination"
	         @change="handleTableChange">
		<template #bodyCell="{ column, record }">
			<template v-if="column.dataIndex === 'operation'">
				<a-space>
					<a-popconfirm
						cancel-text="取消"
						ok-text="确认"
						title="确认删除该乘车人?" @confirm="onDelete(record)">
						<a style="color: red">删除</a>
					</a-popconfirm>
					<a @click="onEdit(record)">编辑</a>
				</a-space>
			</template>
			<template v-else-if="column.dataIndex === 'type'">
        <span v-for="item in PASSENGER_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.type">
            {{ item.desc }}
          </span>
        </span>
			</template>
		</template>
	</a-table>
	<a-modal v-model:visible="visible" cancel-text="取消" ok-text="确认"
	         title="乘车人" @ok="handleOk">
		<a-form :label-col="{span: 4}" :model="passenger" :wrapper-col="{ span: 20 }">
			<a-form-item label="姓名">
				<a-input v-model:value="passenger.name"/>
			</a-form-item>
			<a-form-item label="身份证">
				<a-input v-model:value="passenger.idCard"/>
			</a-form-item>
			<a-form-item label="旅客类型">
				<a-select v-model:value="passenger.type">
					<a-select-option v-for="item in PASSENGER_TYPE_ARRAY" :key="item.code" :value="item.code">
						{{ item.desc }}
					</a-select-option>
				</a-select>
			</a-form-item>
		</a-form>
	</a-modal>
</template>

<script>
import {defineComponent, onMounted, ref} from 'vue';
import {notification} from 'ant-design-vue';
import axios from 'axios';

export default defineComponent({
	name: 'passenger-view',
	setup(){
		/**
		 * 获取乘车人类型
		 */
		const PASSENGER_TYPE_ARRAY = window.PASSENGER_TYPE_ARRAY;
		/**
		 * 判断是否展示新增窗口
		 */
		const visible = ref(false);
		/**
		 * 乘车人列表
		 */
		const passengerList = ref([]);
		/**
		 * 判断是否展示loading
		 */
		let loading = ref(false);

		/**
		 * 乘车人对象
		 */
		let passenger = ref({
			id: undefined,
			memberId: undefined,
			name: undefined,
			idCard: undefined,
			type: undefined,
			createTime: undefined,
			updateTime: undefined,
		});

		// 分页的三个属性名是固定的
		const pagination = ref({
			total: 0,
			current: 1,
			pageSize: 10,
		});

		/**
		 * 乘车人列表头
		 */
		const columns = [
			{
				title: '会员id',
				dataIndex: 'memberId',
				key: 'memberId',
			},
			{
				title: '姓名',
				dataIndex: 'name',
				key: 'name',
			},
			{
				title: '身份证',
				dataIndex: 'idCard',
				key: 'idCard',
			},
			{
				title: '旅客类型',
				dataIndex: 'type',
				key: 'type',
			},
			{
				title: '创建时间',
				dataIndex: 'createTime',
				key: 'createTime',
			},
			{
				title: '更新时间',
				dataIndex: 'updateTime',
				key: 'updateTime',
			},
			{
				title: '操作',
				dataIndex: 'operation'
			}
		];

		/**
		 * 页面完全刷新调用函数
		 */
		onMounted(() => {
			// 页面刚刷新调用获取乘车人列表方法
			handleQuery({
				page: 1,
				size: pagination.value.pageSize
			});
		});

		/**
		 * 点击新增按钮
		 */
		const onAdd = () => {
			// 清空乘车人对象
			passenger.value = {};
			// 展示新增乘车人窗口
			visible.value = true;
		};

		/**
		 * 点击编辑按钮
		 */
		const onEdit = (record) => {
			// 回显数据
			passenger.value = window.Tool.copy(record);
			// 打开编辑窗口
			visible.value = true;
		};

		/**
		 * 新增或编辑乘车人
		 */
		const handleOk = () => {
			// 调用新增乘车人接口
			axios.post('/member/passenger/insert-update', passenger.value).then((r) => {
				const {success, message, content} = r.data;
				// 判断请求是否成功
				if(success){
					notification.success({message: content});
					// 成功后关闭新增窗口
					visible.value = false;
					// 新增成功后重新请求乘车人列表
					handleQuery({
						page: pagination.value.current,
						size: pagination.value.pageSize
					});
				}else{
					notification.error({message: message});
				}
			});
		};

		/**
		 * 查询乘车人列表
		 */
		const handleQuery = (param) => {
			// 判断是否带分页参数，如果不带则赋值
			if(!param){
				param = {
					page: 1,
					size: pagination.value.pageSize
				};
			}
			// 显示loading图标
			loading.value = true;
			// 发送获取乘车人列表请求
			axios.get('/member/passenger/select-list-page', {
				params: {
					page: param.page,
					size: param.size
				}
			}).then((r) => {
				// 请求结束后隐藏loading图标
				loading.value = false;
				const {success, message, content} = r.data;
				// 判断请求是否成功
				if(success){
					// 成功之后保存乘车人列表
					passengerList.value = content.list;
					// 设置分页控件的值
					pagination.value.current = param.page;
					pagination.value.total = content.total;
				}else{
					notification.error({message: message});
				}
			});
		};

		/**
		 * 页面变化调用函数
		 */
		const handleTableChange = (pagination) => {
			// 设置分页
			handleQuery({
				page: pagination.current,
				size: pagination.pageSize
			});
		};

		/**
		 * 删除乘车人
		 */
		const onDelete = (record) => {
			axios.post('/member/passenger/delete', {
				id: record.id,
			}).then((r) => {
				const {success, message} = r.data;
				if(success){
					notification.success({message: '删除成功！'});
					// 设置分页控件的值
					handleQuery({
						page: pagination.value.current,
						size: pagination.value.pageSize,
					});
				}else{
					notification.error({message: message});
				}
			});
		};

		return {
			PASSENGER_TYPE_ARRAY,
			passenger,
			visible,
			passengerList,
			pagination,
			columns,
			handleTableChange,
			handleQuery,
			loading,
			onAdd,
			handleOk,
			onEdit,
			onDelete
		};
	},
});
</script>
